package org.springblade.modules.alimqtt.publish;

import com.alibaba.mqtt.server.ServerProducer;
import com.alibaba.mqtt.server.callback.SendCallback;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.SpringUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Slf4j
public class AliMqttUtil {
	public static void sendMsg(String topic,String message){
		log.info("aliMqtt推送{}===>{}",topic,message);
		try {
			SpringUtil.getBean(ServerProducer.class).sendMessage(topic,message.getBytes(StandardCharsets.UTF_8), new SendCallback() {
				@Override
				public void onSuccess(String msgId) {
					log.info("SendSuccess " + msgId);
				}

				@Override
				public void onFail() {
					log.info("SendFail ");
				}
			});
		} catch (IOException e) {
			log.info("SendFail ");
		}
	}
}
